package cn.sumpu.app.guoguojie.biz.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.transaction.annotation.Transactional;

import cn.sumpu.app.guoguojie.biz.IdentifierBiz;
import cn.sumpu.app.guoguojie.biz.MemberContactBiz;
import cn.sumpu.app.guoguojie.biz.MemberDetailBiz;
import cn.sumpu.app.guoguojie.dao.MemberContactDao;
import cn.sumpu.app.guoguojie.dao.MemberDetailDao;
import cn.sumpu.app.guoguojie.domain.MemberContact;
import cn.sumpu.app.guoguojie.domain.MemberDetail;
import cn.sumpu.app.guoguojie.exception.AppBusinessException;
import cn.sumpu.app.guoguojie.util.Common;
import cn.sumpu.app.guoguojie.util.IdGenerator;

public class MemberContactBizImpl implements MemberContactBiz {
	
	@Resource
	private IdentifierBiz identifierBiz;
	
	private MemberContactDao contactDao;

	private MemberDetailBiz memberDetailBiz;

	private MemberDetailDao memberDetailDao;

	public void setContactDao(MemberContactDao contactDao) {
		this.contactDao = contactDao;
	}

	public void setMemberDetailBiz(MemberDetailBiz memberDetailBiz) {
		this.memberDetailBiz = memberDetailBiz;
	}

	public void setMemberDetailDao(MemberDetailDao memberDetailDao) {
		this.memberDetailDao = memberDetailDao;
	}

	public List<MemberContact> getContactListMember(String member)
			throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("member", member);
		List<MemberContact> list = contactDao.getContactList(map);
		if (list != null && list.size() > 0)
			return list;
		return null;
	}

	public void updateContact(MemberContact contact)
			throws AppBusinessException {

		contactDao.updateContact(contact);
	}

	public MemberContact addContact(MemberContact contact)
			throws AppBusinessException {
		if (!"".equals(contact.getName()) && !"".equals(contact.getValue())) {
			contact.setIdentifier(identifierBiz.generateId("member_contact"));
			contactDao.insert(contact);
		}
		return contact;
	}

	public MemberContact delContact(MemberContact contact)
			throws AppBusinessException {
		contactDao.delContact((contact));
		return contact;
	}

	public void addContactList(List<MemberContact> list)
			throws AppBusinessException {
		for (int i = 0; i < list.size(); i++) {
			contactDao.insert(list.get(i));
		}
	}

	@Transactional(rollbackForClassName = { "cn.sumpu.app.guoguojie.exception.AppBusinessException" }, timeout = 10)
	@Override
	public void addDetail(MemberDetail detail) throws AppBusinessException {
		if (detail == null)
			return;
		MemberDetail d = memberDetailBiz
				.getMemberDetailById(detail.getMember());
		if (d == null) {
			memberDetailDao.insertMemberDetail(detail);
		} else {
			memberDetailDao.updateMemberDetail(detail);
		}
		MemberContact c = new MemberContact();
		c.setMember(detail.getMember());
		contactDao.delContact(c);
		List<MemberContact> cList = detail.getContactList();
		if (cList != null && cList.size() > 0) {
			for (MemberContact contact : cList) {
				contact.setMember(detail.getMember());
				if (!Common.empty(contact.getName())
						&& !Common.empty(contact.getValue())) {
					if (contact.getName().length() > 50)
						contact.setName(contact.getName().substring(0, 49));
					if (contact.getValue().length() > 500)
						contact.setValue(contact.getValue().substring(0, 499));
					contact.setIdentifier(identifierBiz.generateId("member_contact"));
					contactDao.insert(contact);
				}
			}
		}
	}

}
